// MenuModel.cpp
#include "MenuModel.h"
#include <qpalette.h>
MenuModel::MenuModel(QObject *parent) : QAbstractListModel(parent) {}

int MenuModel::rowCount(const QModelIndex &parent) const {
    Q_UNUSED(parent);
    return m_items.size();
}

QVariant MenuModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid() || index.row() >= m_items.size())
        return QVariant();

    const MenuItem &item = m_items[index.row()];
    switch (role) {
    case TextRole: return item.text;
    case ActionRole: return item.action;
    default: return QVariant();
    }
}

QHash<int, QByteArray> MenuModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[TextRole] = "text";
    roles[ActionRole] = "action";
    return roles;
}

void MenuModel::addItem(const QString &text, const QString &action) {
    beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
    m_items.append({text, action});
    endInsertRows();
}

void MenuModel::removeItem()
{
    beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
    m_items.removeAt(0);
    endInsertRows();
}


QColor MenuModel::selfHardLight() {
    QColor baseColor = qApp->palette().highlight().color();
    // 获取颜色分量
    int r = baseColor.red();
    int g = baseColor.green();
    int b = baseColor.blue();

    qDebug() << "selef000" << r << g << b;
    // 对每个通道应用简化的强光公式
    auto calculate = [](int c) -> int {
        return (c > 128)
            ? 255 - ((255 - c) * (255 - c)) / 128  // 高光区域增强
            : (c * c) / 128;                       // 阴影区域增强
    };

    // 计算并应用边界约束
    int resultR = qBound(0, calculate(r), 255);
    int resultG = qBound(0, calculate(g), 255);
    int resultB = qBound(0, calculate(b), 255);
    qDebug() << "selef111" << resultR << resultG << resultB;

    return QColor(resultR, resultG, resultB, baseColor.alpha());
}

QColor MenuModel::hightlight()
{
    QColor c= qApp->palette().color(QPalette::Highlight);
    qDebug() << "ccccc" <<c.red() << c.green() << c.blue();
    return c;
}
